<html>
<head> 管理api</head>
<body>

<p> ---------------token---------------</p>

<div>
    <input type="text" id="input_uid" value="user123" disabled><br/>
    <!--<button id="btn_get_token">获取 token</button> <br/>-->
    <textarea id="show_result" disabled>这里显示结果</textarea><br/>
</div>

<p> ---------------上传明星图片---------------</p>

<div>
    <form id="upload_star">
        <!--<input type="text" name="uid" value="user123"><br>-->
        <!--<input type="text" name="token" id="token" value="token"><br>-->
        <input type="file" name="face" id="select_star_file"><br>
        <input type="text" name="name" id="star_name" placeholder="明星名称"><br>
        <input type="submit" value="上传明星图片">
    </form>
    <div id="star_preview"></div>
</div>
</body>


<script src="/js/jquery.js"></script>
<script src="/js/index.js"></script>
<script>
    var show_result = $('#show_result');
    var uid = $('#input_uid').val();
    var token;
    function showInfo(objOrStr) {
        if (typeof objOrStr === 'object') objOrStr = JSON.stringify(objOrStr);
        show_result.val(objOrStr);
    }

    // 获取token
    $(document).ready(function () {
        uid = $('#input_uid').val();
        getToken(uid, function (data, status) {
            console.log(data);
            if (data.errCode == 0)
                token = data.result.token;
            showInfo(data);
        });
    });

    // ================上传明星脸
    // 选取图片
    var face;
    var name;
    $('#select_star_file').on('change', function (event) {
        face = event.target.files[0];
        if (!face) return;
        var reader = new FileReader();
        reader.readAsDataURL(face);
        reader.onload = function (e) {
            var fileData = e.target.result;
            $('#star_preview').html("<img class='itemView' width='150' padding='5px'/>");
            $('.itemView')[0].src = fileData;
        };
    });
    // 上传
    $('form#upload_star').submit(function (event) {
        event.preventDefault();
        if (!token) {
            showInfo('没有token');
            return false;
        }
        if (!face) {
            showInfo('没有选择明星图片');
            return false;
        }
        name = $('#star_name').val();
        if (!name) {
            showInfo('请输入明星名称 或 描述');
            return false;
        }
        uploadStar(token, face, name, function (err, data) {
            if (err) return showInfo(err);
            if (data.errCode != 0) return showInfo(data);
            showInfo(data.result);
        });
        return false;
    });
</script>
</html>